import { createRouter, createWebHashHistory } from 'vue-router'
import LoginPage from '../components/LoginPage.vue'
import TodoList from '../components/TodoList.vue'
import ScreenshotPage from '../components/ScreenshotPage.vue'
import MainLayout from '../layouts/MainLayout.vue'
import { useAuthStore } from '../stores/auth'

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: LoginPage },
  {
    path: '/',
    component: MainLayout, // ✅ 统一布局
    meta: { requiresAuth: true },
    children: [
      { path: 'todo', component: TodoList },
      { path: 'screenshot', component: ScreenshotPage }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, _, next) => {
  const auth = useAuthStore()
  auth.initializeAuth()

  if (to.meta.requiresAuth && !auth.isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router
